﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using Wamm.Domain.Services.Photo.Parameters;
using WAMM.Domain.Services.Photo.Parameters;

namespace WAMM.Domain.Services.Photo {

    public class PhotoService : IPhotoService {

        public GetDimensionsResponse GetDimensions(GetDimensionsRequest request)
        {
            using (var photoStream = new MemoryStream(request.Photo))
            {
                using (var photo = Image.FromStream(photoStream))
                {

                    return new GetDimensionsResponse
                               {
                                   Width = photo.Width,
                                   Height = photo.Height
                               };
                }
            }
        }

        public ResizeResponse Resize(ResizeRequest request) {

            using (var photoStream = new MemoryStream(request.Photo)) {
                using (var photo = Image.FromStream(photoStream)) {
                    int resizeWidth;
                    int resizeHeight;

                    var photoAspectRatio = (double) photo.Width/photo.Height;
                    var requestedAspectRatio = (double) request.Width/request.Height;

                    if (photoAspectRatio > requestedAspectRatio) {
                        resizeWidth = request.Width;
                        resizeHeight = (int) (resizeWidth/photoAspectRatio);
                    }
                    else {
                        resizeHeight = request.Height;
                        resizeWidth = (int) (resizeHeight*photoAspectRatio);
                    }

                    using (var bmp = new Bitmap(resizeWidth, resizeHeight)) {
                        using (var graphic = Graphics.FromImage(bmp)) {
                            
                            ResizePhoto(photo, resizeWidth, resizeHeight, graphic);

                            return CreateResponse(bmp);
                        }
                    }
                }
            }
        }

        private static ResizeResponse CreateResponse(Image img) {
            var response = new ResizeResponse();
            response.Photo = ConvertImageToByteArray(img, ImageFormat.Jpeg);
            response.Width = img.Width;
            response.Height = img.Height;
            return response;
        }

        private static void ResizePhoto(Image photo, int resizeWidth, int resizeHeight, Graphics graphic) {
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphic.SmoothingMode = SmoothingMode.HighQuality;
            graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphic.CompositingQuality = CompositingQuality.HighQuality;
            graphic.DrawImage(photo, 0, 0, resizeWidth, resizeHeight);
        }

        private static byte[] ConvertImageToByteArray(Image imageToConvert,
                                                      ImageFormat formatOfImage) {
            using (var ms = new MemoryStream()) {
                imageToConvert.Save(ms, formatOfImage);
                return ms.ToArray();
            }
        }
    }
}
